package com.zhilianbao.leyaogo.ui.fragment.shoppingcart;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.bql.utils.EventManager;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.constants.IntentConfig;
import com.zhilianbao.leyaogo.constants.PayConfig;
import com.zhilianbao.leyaogo.events.LeEventCode;
import com.zhilianbao.leyaogo.http.api.PayApi;
import com.zhilianbao.leyaogo.http.callback.DialogCallback;
import com.zhilianbao.leyaogo.model.response.me.MyOrderResponse;
import com.zhilianbao.leyaogo.ui.activity.me.setting.SetPwdActivity;
import com.zhilianbao.leyaogo.ui.fragment.base.BaseOkHttpFragment;
import com.zhilianbao.leyaogo.utils.KeyboardUtils;
import com.zhilianbao.leyaogo.utils.Utils;
import com.zhilianbao.leyaogo.utils.XTipsDialog;
import com.zhilianbao.leyaogo.view.widgets.MoneyTextView;
import com.zhilianbao.leyaogo.view.widgets.PasswordView;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Author: hcy
 * Description:余额支付
 * Created: 2016/11/5 9:50
 */
public class PayByBalanceFragment extends BaseOkHttpFragment {

    @BindView(R.id.tv_title)
    public TextView mTitle;

    @BindView(R.id.mt_pay_account)
    public MoneyTextView mMtPayAccount;

    @BindView(R.id.layout_password)
    public PasswordView mPasswordView;

    private MyOrderResponse mAddedOrderInfo;

    public static PayByBalanceFragment getInstance(MyOrderResponse addedOrderInfo) {
        PayByBalanceFragment payByBalanceFragment = new PayByBalanceFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(IntentConfig.ORDER_INFO, addedOrderInfo);
        payByBalanceFragment.setArguments(bundle);
        return payByBalanceFragment;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_pay_by_balance;
    }

    @Override
    protected void initViewsAndEvents(Bundle savedInstanceState) {
        mTitle.setText(mAddedOrderInfo.getOrderNo());
        mMtPayAccount.setAmount(mAddedOrderInfo.getPaymentPrice());
        KeyboardUtils.showKeyboard(mActivity);
        mPasswordView.setEditCompleListener(num ->

                PayApi.validPayPwd(mActivity, mPasswordView.getSecurityPassworde(), new DialogCallback<Object>(mActivity, getString(R.string.verifying_pwd)) {
                    @Override
                    public void onSuccess(Object o, Call call, Response response) {
                        PayApi.payByAccount(mActivity, mAddedOrderInfo.getSupplierId(), mAddedOrderInfo.getUserId(), Utils.getUserLoginInfo().getUserAlias(), 0, mAddedOrderInfo.getPaymentPrice(), null, mAddedOrderInfo.getOrderId(), mAddedOrderInfo.getOrderNo(), new DialogCallback<Object>(mActivity/*, getString(R.string.paying)*/) {

                            @Override
                            public void onSuccess(Object o, Call call, Response response) {
                                Utils.putLastPayType(PayConfig.YUE_PAY);
                                XTipsDialog.showSuccess(mActivity, getString(R.string.pay_success), () -> start(PaySuccessFragment.getInstance(mAddedOrderInfo.getOrderId())));
                                EventBus.getDefault().post(new EventManager(LeEventCode.MSG_CODE_YUE_PAY_SUCCESS));
                            }

                            @Override
                            public void onError(Call call, Response response, Exception e) {
                                super.onError(call, response, e);
                                mPasswordView.clearSecurityEdit();
                            }
                        });
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        mPasswordView.clearSecurityEdit();
                    }
                })
        );

    }

    @Override
    protected void initToolbarHere() {
        initToolbar(getString(R.string.balance_pay_title));
        mAddedOrderInfo = (MyOrderResponse) getArguments().getSerializable(IntentConfig.ORDER_INFO);
    }

    @OnClick({R.id.tv_forget_password, R.id.rt_pay_now})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_forget_password:
                Utils.startActivity(getActivity(),SetPwdActivity.class);
                break;
            case R.id.rt_pay_now:
                break;
        }
    }

    @Override
    public void leftContainerClick() {
        EventBus.getDefault().post(new EventManager(LeEventCode.MSG_CODE_BALANCE_PAY_CANCEL));
        super.leftContainerClick();
    }

    @Override
    public boolean onBackPressedSupport() {
        EventBus.getDefault().post(new EventManager(LeEventCode.MSG_CODE_BALANCE_PAY_CANCEL));
        return super.onBackPressedSupport();
    }
}
